package cc.lishixi.tool;


import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.*;
import org.apache.pivot.wtk.Window;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class DatTool extends Application.Adapter {
    private Window window;

    @Override
    public void startup(Display display, Map<String, String> properties) throws Exception {
        Theme theme = Theme.getTheme();
        Font defaultFont = theme.getFont();
        Resources resources = new Resources(getClass().getName(), Locale.getDefault());

        // 检查字符是否支持显示指定字符串里的所有字
        String sampleResource = (String) resources.get("testChars");
        String[] availableFontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        java.util.List<String> myFonts3 = new ArrayList<String>();
        myFonts3.add(defaultFont.getFamily());
        myFonts3.add("Microsoft YaHei");
        myFonts3.add("微软雅黑");
        myFonts3.add("simsun");
        myFonts3.add("宋体");
        List<String> strings = Arrays.asList(availableFontFamilyNames);
        List<String> removeList = new ArrayList<String>();
        for (String f : myFonts3) {
            System.out.print("字体：" + f);
            boolean contains = strings.contains(f);
            if (!contains) {
                removeList.add(f);
                System.out.println(",无效");
            } else {
                System.out.println(",有效");
            }
        }
        myFonts3.removeAll(removeList);

        boolean valid = false;
        for (String aMyFonts3 : myFonts3) {
            Font font1 = new Font(aMyFonts3, Font.PLAIN, 12);
            System.out.println("测试字体(" + font1.getName() + ")是否能显示指定字符串.");
            if (font1.canDisplayUpTo(sampleResource) == -1) {
                theme.setFont(font1.deriveFont(Font.PLAIN, 12));
                System.out.println("设置字体：" + font1.getName());
                valid = true;
                break;
            }
        }

        if (!valid) {
            Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
            for (Font font : fonts) {
                if (font.canDisplayUpTo(sampleResource) == -1) {
                    theme.setFont(font.deriveFont(Font.PLAIN, 12));
                    Font font1 = theme.getFont();
                    System.out.println("使用字体 = " + font1.getFamily());
                    break;
                }
            }
        }
        window = new cc.lishixi.tool.MainWindow();

        window.setTitle("Dat Tool");
        window.setMaximized(true);
        window.open(display);

        super.startup(display, properties);
    }

    @Override
    public boolean shutdown(boolean optional) throws Exception {
        if (window != null) window.close();
        return super.shutdown(optional);
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(DatTool.class, args);
    }
}
